package jssia.database.mysql.columns;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Provides the most efficient way in MySQL to represent a column
 * that only has two states, in this case true or false is used.
 * The actual column on the server is represented by a <code>CHAR</code>
 * of length 0.  If the value is null it is considered false, and if
 * the value is an empty string it is considered true;
 *
 */
public final class FlagColumn extends Column {

    /**
     *
     * @param rs the <code>ResultSet</code> that contains a row result for this column.
     * @return true if the value is set to an empty string and false if null.
     * @throws SQLException
     */
    public boolean get(ResultSet rs) throws SQLException {
        return rs.getString(fullyQualifiedName) == null ? false : true;
    }

    /**
     *
     * @param ps the <code>PreparedStatement</code> used to set the parameter <code>flag</code> to.
     * @param index the parameter index for the parameter <code>flag</code>.
     * @param flag the parameter value.
     * @throws SQLException
     */
    public void set(PreparedStatement ps, int index, boolean flag) throws SQLException {
        ps.setString(index, flag == false ? null : "");
    }
}
